package us.mooresr.aircraftscheduling.client.ui;

import java.util.List;

import us.mooresr.aircraftscheduling.client.AdminGatekeeper;
import us.mooresr.aircraftscheduling.client.LoggedInGatekeeper;
import us.mooresr.aircraftscheduling.client.events.ReservationChangedEvent;
import us.mooresr.aircraftscheduling.client.events.ReservationCreatedEvent;
import us.mooresr.aircraftscheduling.shared.action.CreateReservationAction;
import us.mooresr.aircraftscheduling.shared.action.CreateReservationResult;
import us.mooresr.aircraftscheduling.shared.action.UpdateReservationAction;
import us.mooresr.aircraftscheduling.shared.action.UpdateReservationResult;
import us.mooresr.aircraftscheduling.shared.model.Reservation;
import us.mooresr.aircraftscheduling.shared.model.User;
import us.mooresr.aircraftscheduling.client.LoginStateManager;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.PopupView;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ReservationDetailPresenter extends
		PresenterWidget<ReservationDetailPresenter.MyView>
		implements ReservationDetailUiHandlers {

	public interface MyView extends PopupView , HasUiHandlers<ReservationDetailUiHandlers> {
		void clear();
		void setUserData(Boolean isAdmin, Long userId);
		void loadUserPopup(List<User> users);
		void setData(Reservation reservation);
		
	}
	
	private final DispatchAsync dispatcher;
	private final LoginStateManager loginStateManager;

	@Inject
	public ReservationDetailPresenter(final EventBus eventBus, final MyView view,
			DispatchAsync dispatcher, LoginStateManager loginStateManager) {
		
		super(eventBus, view);
		
		this.dispatcher = dispatcher;
		this.loginStateManager = loginStateManager;
		
		getView().setUiHandlers(this);
	}

	public void clear() {
		getView().clear();
		
	}
	
	public void loadUserPopup(List<User> users) {
		getView().setUserData(loginStateManager.isAdmin(), loginStateManager.getCurrentUserId());
		getView().loadUserPopup(users);
	}
	
	public void setData(Reservation reservation) {
		getView().setData(reservation);
	}

	@Override
	protected void onReveal() {

		getView().setUserData(loginStateManager.isAdmin(), loginStateManager.getCurrentUserId());

		
	}
	
	/**
	 *  Request from the dialog to actually create a reservation.  
	 *  The user has hit the OK button.
	 */
	
	@Override
	public void createReservation(Reservation reservation) {
		CreateReservationAction action = new CreateReservationAction(reservation);
		dispatcher.execute(action, new AsyncCallback<CreateReservationResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(CreateReservationResult result) {
				ReservationCreatedEvent.fire(getEventBus(), result.getReservationOut());
				
			}
			
		});
		
	}

	@Override
	public void updateReservation(Reservation reservation) {
		UpdateReservationAction action = new UpdateReservationAction(reservation);
		dispatcher.execute(action, new AsyncCallback<UpdateReservationResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(UpdateReservationResult result) {
				ReservationChangedEvent.fire(getEventBus(), result.getReservationOut());
				
			}
			
		});
		
	}
}
